<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use DB;
use Requests;
use Hash;
use Session;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class MineController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $id = $request->session()->get('id');
        $info = Db::table('member')->where('id',$id)->first();

        return view('home.mine.index',['info'=>$info]);
    }


    //展示个人信息页面
    public function getRudelist(Request $request){
        $id = $request->session()->get('id');
        $info = Db::table('member')->where('id',$id)->first();
        if($info){
            return view('home.mine.rudelist',['info'=>$info]);
        }else{
            return view('home.login.index');
        }


    }
    //修改密码页面
    public function getChange(){
        return view('home.mine.change');
    }
//修改密码
    public function postChan(Request $request){
        $info = $request->input();
        $id = $request->session()->get('id');
        $result =Db::table('member')->where('id',$id)->update(['pwd'=> Hash::make($info['pwd'])]);
        if($result){
             //修改成功
            $status = 1;

        }else{
            //修改失败
            $status = 2;
        }

        return ['status'=>$status];


    }

    //填写昵称
    public function postNickname(Request $request){
        $id = $request->session()->get('id');
        $info = $request->input();
//        dd($info);
        $data =[
            'name'=>$info['nike'],
        ];
        $rec = Db::table('member')->where('id',$id)->update($data);
        if ($rec){
            $status =1;
        }else{
            $status =2;
        }
        return ['status'=>$status];
    }

    public function getTouxiang(){
        return view('home.mine.touxiang');
    }
    //修改上传图片
    public function postUpload(Request $request){
        $id = $request->session()->get('id');
//        $id = 809;
        $file = Input::file('file');
        $allowed_extensions = ["png", "jpg"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return ['error' => 'You may only upload png, jpg or gif.'];
        }
        $destinationPath = 'home/uploads/touxiang/'; //public 文件夹下面建 storage/uploads 文件夹

//        dd($destinationPath);
        $extension = $file->getClientOriginalExtension();

//        $fileName = str_random(10).$id.'.'.$extension;
        $fileName = $id.'.'.$extension;

        $file->move($destinationPath, $fileName);
        $filePath = asset($destinationPath.$fileName);
        $oldImg = $destinationPath.$id.'jpg';
        $oldPng = $destinationPath.$id.'Png';

        @unlink($oldImg);
        @unlink($oldPng);

        //截取存储路径
        $filePath1 = substr($filePath,19);

        $res = DB::table('member')->where('id',$id)->update(['n_img'=>$filePath1]);

        if($res == 0){
            return view('home.mine.index');
        }
    }
    //退出登录
    public  function getLogout(Request $request){
        $request->Session()->flush();
        return view('home.homepage.index');
    }
}
